package org.fuys.coder.domain.config.service.impl;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.fuys.coder.domain.config.model.req.AppConfigAddVersionReq;
import org.fuys.coder.common.exception.BaseException;
import org.fuys.coder.common.exception.BusinessException;
import org.fuys.coder.domain.config.service.BaseConfig;
import org.fuys.coder.infrastructure.dao.config.SecurityConfigMapper;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.common.config.app
 * @className: CoderSecurityConfig
 * @author: WangJing
 * @description: 内容安全相关配置
 * @date: 2024/7/14 18:09
 * @version: 1.0
 */


@Configuration
@Component("securityConfig")
@TableName("security_config")
public class CoderSecurityConfig extends BaseConfig implements Serializable {
    //发布内容可信token过期时间 单位默认为小时
    private Integer publishTokenExpire;
    //点赞或收藏的token过期时间
    private Integer thumbTokenExpire;
    private Integer favTokenExpire;
    private Integer userModelExpire;
    private Integer userModelUpdate;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    @TableField(exist = false)
    @Resource
    private SecurityConfigMapper securityConfigMapper;

    @PostConstruct
    public void initConfigValue(){
        final CoderSecurityConfig coderSecurityConfig = securityConfigMapper.selectOne(new LambdaQueryWrapper<CoderSecurityConfig>()
                .eq(CoderSecurityConfig::getVersion, 1));
        if(coderSecurityConfig==null){
            throw new BaseException("security相关配置注入异常:数据库中不存在相关数据");
        }
        updateAllValue(1,coderSecurityConfig);
    }

    @Override
    protected void setConfigDBValue(Object instance, Integer version) throws RuntimeException {
        if(instance instanceof CoderSecurityConfig){
            CoderSecurityConfig config=(CoderSecurityConfig) instance;
            config.setVersion(version);
            securityConfigMapper.updateById(config);
        }else{
            throw new BusinessException("无法同步到数据库 请检查传递的数据格式!-- 来自安全配置");
        }
    }

    @Override
    protected void switchConfigVersion(Integer version) {
        final CoderSecurityConfig config = securityConfigMapper.selectOne(new LambdaQueryWrapper<CoderSecurityConfig>()
                .eq(CoderSecurityConfig::getVersion, version));
        if(config==null){
            throw new BusinessException("没有此版本号的配置文件 请核对之后再次切换 或添加新版本 --来自安全配置");
        }
        this.setVersion(version);
        this.updateAllValue(version,config);
    }

    @Override
    protected void addConfigVersion(AppConfigAddVersionReq req) {
        CoderSecurityConfig securityConfig=new CoderSecurityConfig();
        securityConfig.setVersion(req.getVersion());
        securityConfig.setDescription(req.getDesc());
        securityConfig.setThumbTokenExpire(req.getThumbTokenExpire());
        securityConfig.setFavTokenExpire(req.getFavTokenExpire());
        securityConfig.setPublishTokenExpire(req.getPublishTokenExpire());
        securityConfig.setUserModelUpdate(req.getUserModelUpdate());
        securityConfig.setUserModelExpire(req.getUserModelExpire());
        this.securityConfigMapper.insert(securityConfig);
    }

    @Override
    protected List<? extends BaseConfig> getConfigList() {
        return this.securityConfigMapper.selectList(new LambdaQueryWrapper<>());
    }

    private void updateAllValue(Integer version,CoderSecurityConfig config){
        this.setVersion(version);
        this.setFavTokenExpire(config.getFavTokenExpire());
        this.setPublishTokenExpire(config.getPublishTokenExpire());
        this.setThumbTokenExpire(config.getThumbTokenExpire());
        this.setDescription(config.getDescription());
        this.setUserModelExpire(config.getUserModelExpire());
        this.setUserModelUpdate(config.getUserModelUpdate());
    }

    public Integer getPublishTokenExpire() {
        return publishTokenExpire;
    }

    public void setPublishTokenExpire(Integer publishTokenExpire) {
        this.publishTokenExpire = publishTokenExpire;
    }

    public Integer getThumbTokenExpire() {
        return thumbTokenExpire;
    }

    public void setThumbTokenExpire(Integer thumbTokenExpire) {
        this.thumbTokenExpire = thumbTokenExpire;
    }

    public Integer getFavTokenExpire() {
        return favTokenExpire;
    }

    public void setFavTokenExpire(Integer favTokenExpire) {
        this.favTokenExpire = favTokenExpire;
    }

    public Integer getUserModelExpire() {
        return userModelExpire;
    }

    public void setUserModelExpire(Integer userModelExpire) {
        this.userModelExpire = userModelExpire;
    }

    public Integer getUserModelUpdate() {
        return userModelUpdate;
    }

    public void setUserModelUpdate(Integer userModelUpdate) {
        this.userModelUpdate = userModelUpdate;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof CoderSecurityConfig)) return false;
        CoderSecurityConfig config = (CoderSecurityConfig) o;
        return Objects.equals(getPublishTokenExpire(), config.getPublishTokenExpire()) && Objects.equals(getThumbTokenExpire(), config.getThumbTokenExpire()) && Objects.equals(getFavTokenExpire(), config.getFavTokenExpire());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getPublishTokenExpire(), getThumbTokenExpire(), getFavTokenExpire());
    }
}
